package com.lichuang.dedao.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lichuang.dedao.user.entity.UserCoupon;
import com.lichuang.dedao.user.enums.CouponIsUsed;
import com.lichuang.dedao.user.mapper.UserCouponMapper;
import com.lichuang.dedao.user.service.IUserCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户卡券 服务实现类
 * </p>
 *
 * @author hanghang
 * @since 2019-07-15
 */
@Service
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements IUserCouponService {

    @Override
    public Integer issueCoupon(Integer userId, Integer couponId, LocalDate cutTime) {
        UserCoupon userCoupon = new UserCoupon();
        userCoupon.setUserId(userId)
                .setCouponId(couponId)
                .setCutTime(cutTime)
                .setCreateTime(LocalDateTime.now());
        Integer result = baseMapper.insert(userCoupon);
        return result;
    }

    @Override
    public Integer useCoupon(Integer userId, Integer userCouponId) {
        UserCoupon userCouponParam = new UserCoupon();
        userCouponParam.setUserId(userId)
                .setCouponId(userCouponId);
        QueryWrapper queryWrapper = new QueryWrapper(userCouponParam);
        UserCoupon userCoupon = new UserCoupon();
        userCoupon.setIsUsed(CouponIsUsed.USED.getValue())
                .setUsedTime(LocalDateTime.now());
        Integer result = baseMapper.update(userCoupon, queryWrapper);
        return result;
    }

    @Override
    public List<UserCoupon> selectUserCoupons(Integer userId) {
        List<UserCoupon> userCoupons = baseMapper.selectUserCoupons(userId,LocalDate.now());
        return userCoupons;
    }
}
